package com.jhy.common.shardingjdbc.algorithm;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

@Slf4j
public class IdPreciseAlgorithm implements PreciseShardingAlgorithm<Date> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        int hours = new DateTime(preciseShardingValue.getValue()).hour(true);
        int no = hours % 2;
        //补充说明：其实tableNames中的值就是配置文件中的actual-data-nodes中配置的所有表，此处返回的是要进行查询的表
        log.info("表名后缀值：{} " , no);
        return preciseShardingValue.getLogicTableName() + "_" + no;

    }
}
